import mysql.connector
from dbutils.pooled_db import PooledDB
#import Config

config_file_path = 'Config/config.erpconfig'


def load_sql_config(file_path):
    config_data = {}
    with open(file_path, 'r') as file:
        for line in file:
            if 'erp_sql_host' in line:
                erp_sql_host = line.split('=')[1].strip().strip("'")
                config_data.update({'erp_sql_host': erp_sql_host})
            if 'erp_sql_port' in line:
                erp_sql_port = line.split('=')[1].strip().strip("'")
                config_data.update({'erp_sql_port': erp_sql_port})
            if 'erp_sql_account' in line:
                erp_sql_account = line.split('=')[1].strip().strip("'")
                config_data.update({'erp_sql_account': erp_sql_account})
            if 'erp_sql_password' in line:
                erp_sql_password = line.split('=')[1].strip().strip("'")
                config_data.update({'erp_sql_password': erp_sql_password})
            if 'erp_sql_database_name' in line:
                erp_sql_database_name = line.split('=')[1].strip().strip("'")
                config_data.update({'erp_sql_database_name': erp_sql_database_name})
                break
    return config_data


def mysql_connect():
    config_data = load_sql_config(config_file_path)
    conn = mysql.connector.connect(
        # host="127.0.0.1",
        host=config_data['erp_sql_host'],
        user=config_data['erp_sql_account'],
        password=config_data['erp_sql_password'],
        database=config_data['erp_sql_database_name']
    )
    return conn
